package com.example.administrator.jokeyboarddemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.example.administrator.jokeyboarddemo.keyboard.KeyBoardCallBack;
import com.example.administrator.jokeyboarddemo.keyboard.KeyBoardType;
import com.example.administrator.jokeyboarddemo.keyboard.KeyboardView;
import com.example.administrator.jokeyboarddemo.keyboard.LayoutEditText;

public class TestActivity extends Activity implements LayoutEditText.OnItemEditClick,View.OnClickListener {


    private KeyboardView keyboardView;

    private LayoutEditText layoutEdits;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initView();
    }

    private void initView() {
        layoutEdits = (LayoutEditText)findViewById(R.id.layout_edits);
        keyboardView = (KeyboardView)findViewById(R.id.keyboard);
        keyboardView.setOnClickKeyFinishListener(keyBoardCallBack);
        keyboardView.showedBoardView(KeyBoardType.CHINESE);
//        layoutEdits.signEditText(10);
        layoutEdits.setOnItemClick(this);
        findViewById(R.id.chinese).setOnClickListener(this);
        findViewById(R.id.english).setOnClickListener(this);
        findViewById(R.id.math).setOnClickListener(this);
    }

    /**键盘弹出**/

    private void showKeyBoard(){
        if(keyboardView.getVisibility()==View.GONE){
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    KeyBoardCallBack keyBoardCallBack = new KeyBoardCallBack() {
        @Override
        public void getFinishEditValue(String text) {
            layoutEdits.getCurrentEditText().getText().append(text);
        }

        @Override
        public void deleteKeyBoard() {
            int currentSelect = layoutEdits.getCurrentEditText().getSelectionStart();
            if(layoutEdits.getCurrentEditText().getText().length()!=0){
                layoutEdits.getCurrentEditText().getText().delete(currentSelect-1,currentSelect);
            }
            layoutEdits.deleteEditTextSpan();
        }

        @Override
        public void deleteLongKeyBoard() {
            int currentSelect = layoutEdits.getCurrentEditText().getSelectionStart();
            if(layoutEdits.getCurrentEditText().getText().length()!=0){
                layoutEdits.getCurrentEditText().getText().delete(0,currentSelect);
                layoutEdits.getCurrentEditText().getText().clear();
            }

        }

        @Override
        public void onNext() {
            layoutEdits.setNextEditText();

        }

        @Override
        public void onLast() {
            layoutEdits.setLastEditText();
        }

        @Override
        public void closeKeyBoard() {
            Log.e("aaa","关闭键盘");
            keyboardView.setVisibility(View.GONE);
        }

        @Override
        public void showMolecular() {

        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(keyboardView.getVisibility() == View.VISIBLE){
                keyboardView.setVisibility(View.GONE);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onItemClick(int currentClick) {
        showKeyBoard();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chinese:
                keyboardView.showedBoardView(KeyBoardType.CHINESE);
                break;
            case R.id.english:
                keyboardView.showedBoardView(KeyBoardType.ENGLISH);
                break;
            case R.id.math:
                keyboardView.showedBoardView(KeyBoardType.MATH);
                break;
        }
    }


}
